package pancake.function;

import java.net.Authenticator;
import java.net.URL;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DbAccount{
 
	private Context context;
	private String name;
	private String pwd;
	private String type;
	DBHelper helper;
	SQLiteDatabase db;
	public DbAccount(String name, String pwd, String type,Context context)
	{
		this.context = context;
		this.name = name;
		this.pwd = pwd;
		this.type = type;
		this.helper = new DBHelper(context,"Pancake.db",null,1);
		this.db = helper.getWritableDatabase();
	 
	}
	public DbAccount(Context context)
	{
		this.helper = new DBHelper(context,"Pancake.db",null,1);
		this.db = helper.getWritableDatabase();
	}
 
	public Boolean IsValid()
	{
		if(this.name.length()==0)
		{
			Popup("请输入你的用户名");
			return false;
		}
		if(this.pwd.length()==0)
		{
			Popup("请输入你的密码");
			return false;
		}
		if(!this.IsAuthz())
		{
			Popup("用户名或密码不正确");
			return false;
		}
		return true;
	}

	public String[] GetAccounts()
	{
		String[] accounts=null;
		Cursor cur =db.rawQuery("select * from "+DBHelper.TABLE,null);
		if(cur.moveToFirst())
		{
			 ArrayList<String> myaccount = new ArrayList<String>();
			 do
			 {
				 String type = cur.getString(cur.getColumnIndex(DBHelper.TYPE));
				 String name = cur.getString(cur.getColumnIndex(DBHelper.PWD));
				 myaccount.add(type+":"+name);
			 }
			 while(cur.moveToNext());
			 accounts = (String[]) myaccount.toArray(new String[0]);
		 }
		 cur.close();
		 return accounts;
	}
	
	public void Insert()
	{
		String[] columns = {DBHelper.TYPE,DBHelper.NAME,DBHelper.PWD};
		String selection = DBHelper.TYPE +"= ?";
		String[] selectionArgs = {this.type};
		Cursor c = db.query(DBHelper.TABLE, columns, selection, selectionArgs, null, null, null);
		if(!c.moveToFirst())
		{
			ContentValues values = new ContentValues();
			values.put(DBHelper.TYPE,this.type);
			values.put(DBHelper.NAME,this.name);
			values.put(DBHelper.PWD,this.pwd);
			db.insert(DBHelper.TABLE, DBHelper.TYPE, values);
			c.close();
		}
		else
		{
			Popup(this.type + "帐户已经存在了.");
		}
		 
	}
	public String[] GetAccount(String type)
	{
		String[] columns = {DBHelper.TYPE,DBHelper.NAME,DBHelper.PWD};
		String selection = DBHelper.TYPE +"= ?";
		String[] selectionArgs = {type};
		Cursor c = db.query(DBHelper.TABLE, columns, selection, selectionArgs, null, null, null);
		String name = c.getString(c.getColumnIndex(DBHelper.NAME));
		String pwd = c.getString(c.getColumnIndex(DBHelper.PWD));
		return new String[]{name,pwd};
	}
	private Boolean IsAuthz()
	{
		Boolean flag = false;
		Authenticator.setDefault(new MyAuth(this.name,this.pwd));
		try{
			URL url = new URL(MyFanfou.VerifyCredentials);
			flag = true;
		}
		catch(Exception e){
			
		}
		return flag;
	}
	
	private void Popup(String message)
	{
		AlertDialog.Builder ad = new AlertDialog.Builder(context);  
		ad.setTitle("输入不正确");  
		ad.setMessage(message);   
		ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {  
		   public void onClick(DialogInterface dialog, int id) {  
		     dialog.cancel(); 
		}  
		});  
		ad.show();
	}
	public void Delete(String type)
	{
		String[] columns = {DBHelper.TYPE,DBHelper.NAME,DBHelper.PWD};
		String selection = DBHelper.TYPE +"= ?";
		String[] selectionArgs = {this.type};
		db.delete(DBHelper.TABLE, selection, selectionArgs);
	}
}
